
#include "CCheckListBox.h"



DWORD WINAPI SearchFileThreadFunc( LPVOID pFileListBox )
{
	CCheckListBox* pThis = (CCheckListBox*)pFileListBox;

	TCHAR tcFilePath[MAX_PATH];	
	TCHAR tcPath[MAX_PATH];
	memset(tcFilePath,0,sizeof(TCHAR)*MAX_PATH);
	swprintf(tcFilePath,L"%s\\*.*",pThis->m_sCurDir.c_str());

	
	WIN32_FIND_DATA FindData = {0};

	HANDLE hFindFile = FindFirstFile(tcFilePath, &FindData);
	if ( hFindFile != INVALID_HANDLE_VALUE )
	{
		do {		
			wstring strExtension;
			int nCount = pThis->m_pVeExtName->size();
			int i;
			if( !(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )		// add folder
			{
				CUtil::GetFileExtension(FindData.cFileName,strExtension);
				if(!strExtension.length())
				{
					continue;
				}
				for(i = 0; i< nCount; i++)
				{
					if(strExtension == pThis->m_pVeExtName->at(i))
					{
						memset(tcPath,0,sizeof(TCHAR)*MAX_PATH);
						swprintf(tcPath,L"%s\\%s",pThis->m_sCurDir.c_str(),FindData.cFileName);
						//Item.nType = FILE_TYPE;		
						//Item.sFilePath = tcPath;
						//Item.sFileFullName = FindData.cFileName;
						//wstring fileName = 	FindData.cFileName;
						//CUtil::TrimFileExtension(fileName);	
						//Item.sFileName = fileName;
						//pThis->m_listItems.push_back(Item);
						//pThis->UpDateListBox();
				
						pThis->InsertItem(tcPath,NULL,SINGLE_LINE_TEXT,50);
						InvalidateRect(pThis->GetContainer()->m_hWnd,NULL,FALSE);
						
					}
				}
			} 
		} while ( !pThis->m_bStop && FindNextFile(hFindFile, &FindData));
		FindClose( hFindFile );
	}
	PostMessage(pThis->GetContainer()->m_hWnd,WM_SEARCH_END,0,0);
	return 0;
}


void CCheckListBox::SetExtName(vector<wstring>*	pVeExtName)
{
	m_pVeExtName = pVeExtName;
}
void CCheckListBox::RunSearch()
{

	StopSearch();
	m_bStop = FALSE;
	DWORD threadID;
     
	m_hThread = CreateThread( 
		NULL,              // default security attributes
		0,                 // use default stack size  
		SearchFileThreadFunc,        // thread function 
		this,             // argument to thread function 
		0,                 // use default creation flags 
		&threadID);  
}
CCheckListBox::CCheckListBox(DWORD dwStyle, wstring strCaption,const CRect &rc, UINT nID):CListBoxEx(dwStyle,strCaption,rc, nID)
{
m_pCheckBoxpt.x=0;
m_pCheckBoxpt.y=0;
m_hThread=NULL;


}
void CCheckListBox::StopSearch()
{
	m_bStop = TRUE;
	if(m_hThread)
	{
		Sleep(100);
		CloseHandle(m_hThread);
		m_hThread = NULL;
	}

}
void CCheckListBox::DrawItem(CDC *pDC,CRect rcItem, LISTBOXEX_ITEM *pItem, BOOL bSelected)
{
	COLORREF clrOld;
	
	CRect rcImage(0,0,0,0);
	

	BEGIN_FONT_SELECTION

		pDC->SetBkMode(TRANSPARENT);

	if(bSelected && m_hSelBackgnd)
	{
		CRect rcTmp = rcItem;
		/*rcTmp.right -= 5;
		rcTmp.DeflateRect(0,-3);
		rcTmp.OffsetRect(0,7);*/

		//pDC->AlphaBlendEx(rcTmp,m_hSelBackgnd,CRect(0,0,320,50),150);

		clrOld = pDC->SetTextColor(m_clrTxtActive);
	}
	else
	{
		clrOld = pDC->SetTextColor(m_clrTxtNormal);
	}

	if(m_dwExtendedStyles & LISTBOXEX_STYLE_GRIDLINES/* && !bSelected*/)
	{
		CPoint ptStart;
		ptStart.x = rcItem.left;
		ptStart.y = rcItem.top + rcItem.Height();

		CPoint ptEnd;
		ptEnd.x = rcItem.right-5;
		ptEnd.y = ptStart.y;

		DrawLine(pDC,ptStart,ptEnd);
	}

	CPoint ptCheckBox = rcItem.TopLeft();

	if(m_dwExtendedStyles & LISTBOXEX_STYLE_CHECKBOX)
	{
		ptCheckBox.x += m_pCheckBoxpt.x*m_pContainer->GetZoom();
		ptCheckBox.y +=m_pCheckBoxpt.y*m_pContainer->GetZoom();
		ptCheckBox.x += ITEM_CHECKBOX_WIDTH;
		if (bSelected)
		{
			m_ImgLstCheck->Draw(pDC,0,ptCheckBox);	
		}
		else
		m_ImgLstCheck->Draw(pDC,1,ptCheckBox);	
	}

	if(m_dwExtendedStyles & LISTBOXEX_STYLE_IMAGES)
	{
		if(m_pImgLstIcon)
		{
			/*IMAGEINFO imgInfo = {0};
			m_pImgLstIcon->GetImageInfo(0,&imgInfo);
			rcImage = imgInfo.rcImage;*/

			CSize sz;

			m_pImgLstIcon->GetIconSize(sz);
			rcImage.bottom = sz.cy;
			rcImage.right = sz.cx;

			CPoint pt = ptCheckBox;
			pt.x += ITEM_IMAGE_PADDING_LEFT;
			pt.y = rcItem.top;
			pt.y += (rcItem.Height() - rcImage.Height())/2;

			m_pImgLstIcon->Draw(pDC,pItem->uiImage,pt);			
		}
	}

	if(pItem->nStyle == NORMAL_TEXT)
	{

		CRect rc = rcItem;

		if(rcImage.Width())
		{
			rc.DeflateRect(rcImage.Width() + ITEM_IMAGE_PADDING_LEFT + ITEM_IMAGE_PADDING_RIGHT,0,0,0);
		}

		if(m_dwExtendedStyles & LISTBOXEX_STYLE_CHECKBOX)
		{
			rc.left += ITEM_PADDING_LEFT + ITEM_CHECKBOX_WIDTH;
		}
		else
		{
			rc.left += ITEM_PADDING_LEFT;
		}

		if(!pItem->bHeightSpecified)
		{
			rc.top += ITEM_PADDING_TOP;
		}

		rc.OffsetRect(m_nTextLeftMargin,0);

		pDC->DrawText(pItem->sItemText.c_str(),pItem->sItemText.length(),&rc,
			m_dwAlign/*DT_LEFT|DT_WORDBREAK|DT_VCENTER*/);
	}
	else if(pItem->nStyle == SINGLE_LINE_TEXT)
	{
		CRect rc = rcItem;
		if(rcImage.Width())
		{
			rc.DeflateRect(rcImage.Width() + ITEM_IMAGE_PADDING_LEFT + ITEM_IMAGE_PADDING_RIGHT,0,0,0);
		}
		if(m_dwExtendedStyles & LISTBOXEX_STYLE_CHECKBOX)
		{
			rc.left += ITEM_PADDING_LEFT + ITEM_CHECKBOX_WIDTH;
		}
		else
		{
			rc.left += ITEM_PADDING_LEFT;
		}

		if(!pItem->bHeightSpecified)
			rc.top += ITEM_PADDING_TOP;

		//See if we can fit the text in one line
		//TCHAR szBuffer[_MAX_PATH];
		//memset(szBuffer,0,_MAX_PATH);

		//_tcscpy(szBuffer,pItem->sItemText);
		/*
		if(PathCompactPath(pDC->GetSafeHdc(),szBuffer,rc.Width()))
		{
		pDC->DrawText(szBuffer,_tcslen(szBuffer),&rc,
		DT_LEFT|DT_SINGLELINE|DT_VCENTER);	
		}
		else*/

		{
			wstring strTmp;
			TCHAR szID[10] = {0};
			_itot(pItem->nItemNo+1,szID,10);
			wstring strText = pItem->sItemText;
			CUtil::TrimPathLeft(strText);

			strTmp = szID;
			strTmp += L". ";
			strTmp += strText;
			pDC->DrawText(strTmp.c_str(),strTmp.length(),&rc,m_dwAlign); 

		}
	}

	pDC->SetTextColor(clrOld);

	END_FONT_SELECTION

}

